package com.example.yls.cnbetaapp.activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import com.example.yls.cnbetaapp.R;
import com.example.yls.cnbetaapp.adapter.MenuAdapter;
import com.example.yls.cnbetaapp.adapter.MessageAdapter;
import com.example.yls.cnbetaapp.amqp.RPCClient;
import com.example.yls.cnbetaapp.entity.ContentModel;
import com.example.yls.cnbetaapp.httpService.MessageService;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
    private Context mContext;
    private MessageAdapter mAdapter = null;
    private ListView list_message;
    private Handler handler;
    private WebView webView;
    private List<ContentModel> mList = new ArrayList<ContentModel>();
    private ListView mListView;
    MessageService getMessage = new MessageService();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        webView = new WebView(this);
        list_message = (ListView) findViewById(R.id.list_message);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                mAdapter = (MessageAdapter) msg.obj;
                list_message.setAdapter(mAdapter);
                //添加资讯列表项的点击事件
                list_message.setOnItemClickListener(MainActivity.this);
            }
        };
        new Thread(new Runnable() {
            @Override
            public void run() {
                //将handler传递过去，请求到数据列表后再回调上面的handler方法
                getMessage.getMessageAdapter(mContext, 1,handler);
            }
        }).start();
        //初始化侧边栏的数据，即集合mlist数据
        initData();
        mListView = (ListView) findViewById(R.id.left_listview);
        MenuAdapter adapter = new MenuAdapter(this,mList);
        mListView.setAdapter(adapter);
        final DrawerLayout mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerlayout);
        //给左上角图标添加点击事件，当点击时打开侧边栏
        findViewById(R.id.leftmenu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawerLayout.openDrawer(Gravity.LEFT);
            }
        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0://首页
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                getMessage.getMessageAdapter(mContext, 1,handler);
                            }
                        }).start();
                        break;
                    case 1://电影
                        new Thread(new Runnable() {
                            @Override
                            public void run() {

                                getMessage.getMessageAdapter(mContext, 2,handler);
                            }
                        }).start();
                        break;
                    case 2://音乐
                        new Thread(new Runnable() {
                            @Override
                            public void run() {

                                getMessage.getMessageAdapter(mContext, 3,handler);
                            }
                        }).start();
                        break;
                    case 3://游戏
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                getMessage.getMessageAdapter(mContext, 4,handler);
                            }
                        }).start();
                        break;
                    case 4://趣闻
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                getMessage.getMessageAdapter(mContext, 5,handler);
                            }
                        }).start();
                        break;
                    case 5://软件
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                getMessage.getMessageAdapter(mContext, 6,handler);
                            }
                        }).start();
                        break;
                    default:
                        break;
                }
                //点击任一项item项，都关闭左侧菜单
                mDrawerLayout.closeDrawer(Gravity.LEFT);
            }
        });
}


    private void initData() {

        mList.add(new ContentModel(R.mipmap.home, "首页", 1));
        mList.add(new ContentModel(R.mipmap.movie, "电影", 2));
        mList.add(new ContentModel(R.mipmap.music, "音乐", 3));
        mList.add(new ContentModel(R.mipmap.games, "游戏", 4));
        mList.add(new ContentModel(R.mipmap.news, "趣闻", 5));
        mList.add(new ContentModel(R.mipmap.software,"软件", 6));
    }
    //list_message项的点击事件
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent =new Intent(MainActivity.this, ContentActivity.class);
        intent.putExtra("mid",(String)((TextView)view.findViewById(R.id.mid)).getText());
        startActivity(intent);
    }

}